package com.bi.bitest.bao.utils;

import com.bi.bitest.bao.Constants;
import com.bi.bitest.bao.entity.AlbumDetail.Album_AlbumDetail;
import com.bi.bitest.bao.entity.AlbumDetail.Tracks;
import com.bi.bitest.bao.entity.AlbumPlay.AlbumPlay;
import com.bi.bitest.bao.entity.DiscoverCategory;
import com.bi.bitest.bao.entity.discoverrecommend.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created
 * Author:bi[FR]
 * Date:15-10-20
 */
public class EntityParseUtil {
    private EntityParseUtil(){

    }

    public static AlbumPlay parseAlbum_Play(JSONObject json){
        AlbumPlay ret=null;
        if(json!=null)
        {
            ret=new AlbumPlay();
            try {
                ret.parseJSON(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }


    public static Album_AlbumDetail parseAlbum_AalbumDetail(JSONObject json){
        Album_AlbumDetail ret=null;
        if(json!=null)
        {
            ret=new Album_AlbumDetail();
            try {
                JSONObject obj_album=json.getJSONObject("album");
                ret.parseJSON(obj_album);
                JSONObject obj_tracks=json.getJSONObject("tracks");
                JSONArray array=obj_tracks.getJSONArray("list");
                int len=array.length();
                if(len>0) {
                    List<Tracks>tracks=new ArrayList<Tracks>();
                    for (int i = 0; i < len; i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        Tracks track = new Tracks();
                        //利用实体类内部JSON 解析内部调用  简洁当实体类添加字段  也好管理
                        track.parseJSON(jsonObject);
                        tracks.add(track);
                    }
                    ret.setList(tracks);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        return ret;
    }

    public static List<DiscoverCategory> parseDiscoverCategories(JSONObject json){
        List<DiscoverCategory>ret=null;
         if(json!=null){
             try {
                 int code=json.getInt("ret");
                 if(code== Constants.TASK_RESULT_OK){
                   ret=new LinkedList<DiscoverCategory>();
                     JSONArray array=json.getJSONArray("list");
                     int len=array.length();
                     if(len>0){
                         for(int i=0;i<len;i++){
                             JSONObject jsonObject=array.getJSONObject(i);
                             DiscoverCategory discoverCategory=new DiscoverCategory();
                            //利用实体类内部JSON 解析内部调用  简洁当实体类添加字段  也好管理
                             discoverCategory.parseJSON(jsonObject);
                             ret.add(discoverCategory);
                         }
                     }
                 }
             } catch (JSONException e) {
                 e.printStackTrace();
             }
         }

    return ret;
    }


    public static List<DiscoverRecommendItem> parseDiscoverRecommends(JSONObject jsonObject) {
        List<DiscoverRecommendItem> ret = null;
        if(jsonObject != null ){
            try {
                int code = jsonObject.getInt("ret");
                if(code == Constants.TASK_RESULT_OK){
                    ret = new ArrayList<DiscoverRecommendItem>();

                    // TODO 小编推荐 内容的解析
                    JSONObject object = jsonObject.getJSONObject("editorRecommendAlbums");
                    DiscoverRecommendAlbums editorReconmmend = new DiscoverRecommendAlbums();
                    editorReconmmend.parseJSON(object);
                    ret.add(editorReconmmend);
                    // TODO 解析 精品听单
                    JSONObject objecttingdan = jsonObject.getJSONObject("specialColumn");
                    DiscoverRecommendSpecial specialReconmmend = new DiscoverRecommendSpecial();
                    specialReconmmend.parseJSON(objecttingdan);
                    ret.add(specialReconmmend);
                    //TODO 解析listView的headerView
                    JSONObject objectHeader = jsonObject.getJSONObject("focusImages");
                    DiscoverRecommendHeader header = new DiscoverRecommendHeader();
                    header.parseJSON(objectHeader);
                    ret.add(header);

                    // TODO 解析 发现惊奇

                    JSONObject objectfaxian = jsonObject.getJSONObject("discoveryColumns");
                    DiscoverRecommendColumns columns = new DiscoverRecommendColumns();
                    columns.parseJSON(objectfaxian);
                    ret.add(columns);

                    // TODO 热门推荐 内容的解析

                    object = jsonObject.getJSONObject("hotRecommends");
                    JSONArray array = object.getJSONArray("list");
                    int len = array.length();
                    for(int i=0;i<len;i++){
                        JSONObject object1 = array.getJSONObject(i);
                        DiscoverRecommendAlbums   albums = new DiscoverRecommendAlbums();
                        albums.parseJSON(object1);
                        ret.add(albums);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }
}
